#This is to get:
#TableA6
#TableA7

rm(list = ls())

#library("foreign")
library("haven")
library("lfe")
library("stargazer")
library("clusterSEs")


setwd("C:/Users/bogdanp/Dropbox/Legacies_Central_Europe/")
setwd("/Users/bgpopescu/Dropbox/Legacies_Central_Europe")

#Reading STATA data (original file)
dta<-read_dta("./data/lits_data.dta")

dta$bfe1<-ifelse(dta$Ott_Habs_brd_NEAR_FID==1, 1, 0)
dta$bfe2<-ifelse(dta$Ott_Habs_brd_NEAR_FID==9, 1, 0)
dta$bfe3<-ifelse(dta$Ott_Habs_brd_NEAR_FID==10, 1, 0)
dta$bfe4<-ifelse(dta$Ott_Habs_brd_NEAR_FID==11, 1, 0)
dta$bfe5<-ifelse(dta$Ott_Habs_brd_NEAR_FID==12, 1, 0)
dta$bfe6<-ifelse(dta$Ott_Habs_brd_NEAR_FID==13, 1, 0)
dta$bfe7<-ifelse(dta$Ott_Habs_brd_NEAR_FID==14, 1, 0)
dta$bfe8<-ifelse(dta$Ott_Habs_brd_NEAR_FID==15, 1, 0)
summary(dta$Ott_Habs_brd_distance)

#* corruption police */
#Model1
res_q801a<-felm(q801a~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
     bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
     |0|0|PSU_name, 
   data=dta)
summary(res_q801a)
res_q801a_se<-res_q801a$cse


#Model2
#/* corruption official documents */
res_q801b<-felm(q801b~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name, 
              data=dta)
summary(res_q801b)
res_q801b_se<-res_q801b$cse


#Model3
#/* corruption education*/
res_q801d<-felm(q801d~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name, 
              data=dta)
summary(res_q801d)
res_q801d_se<-res_q801d$cse

#Model4
#/* corruption education: Public Education and Vocational*/
res_q801e<-felm(q801e~treat + point_x + point_y+
              Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
              |0|0|PSU_name,
              data=dta)
summary(res_q801e)
res_q801e_se<-res_q801e$cse



#Model5
#/* corruption medical*/
res_q801f<-felm(q801f~treat + point_x + point_y+
                  Ott_Habs_brd_distance + 
                  age_pr + gender_pr + q109_1 + urban + 
                  bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name,
                data=dta)
summary(res_q801f)
res_q801f_se<-res_q801f$cse

#Model6
#/* corruption benefits*/
res_q801g<-felm(q801g~treat + point_x + point_y +
              Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
              |0|0|PSU_name,
              data=dta)
summary(res_q801g)
res_q801g_se<-res_q801g$cse


#Model7
res_q801h<-felm(q801h~treat + point_x + point_y +
              Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
              |0|0|PSU_name, 
              data=dta)
summary(res_q801h)
res_q801h_se<-res_q801h$cse

#Model8
#/* govt officials*/
res_q814c<-felm(q814c~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name, 
              data=dta)
summary(res_q814c)
res_q814c_se<-res_q814c$cse


#Model9
#/* local officials */
res_q814d<-felm(q801d~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name, 
              data=dta)
summary(res_q814d)
res_q814d_se<-res_q814d$cse


public_opinion=stargazer(res_q801a,
                         res_q801b,
                         res_q801d,
                         res_q801e,
                         res_q801f,
                         res_q801g,
                         res_q801h,
                         res_q814c,
                         res_q814d,
                         object.names = F,
                         column.labels= c("\\shortstack{Payments\\\\Road\\\\Police}", 
                                          "\\shortstack{Payments\\\\to get\\\\Offical Doc.}", 
                                          "\\shortstack{Payments\\\\to receive\\\\Public Edu.\\\\Primary or\\\\Sec.}",
                                          "\\shortstack{Payments\\\\to receive\\\\Public Edu.\\\\Vocational}",
                                          "\\shortstack{Payments\\\\to receive\\\\Public Health\\\\Treatment}",
                                          "\\shortstack{Payments\\\\to receive\\\\Unemployment\\\\Benefits}",
                                          "\\shortstack{Payments\\\\to receive\\\\Social Sec.\\\\Benefits}",
                                          "\\shortstack{How many\\\\Gov. officials\\\\are\\\\Corrupt?}",
                                          "\\shortstack{How many\\\\Loc. Gov. officials\\\\are\\\\Corrupt?}"),
                         keep = c("treat"),
                         star.char = c("+", "*", "**", "***"),
                         star.cutoffs = c(0.1, 0.05, 0.01, 0.001),
                         covariate.labels=c("Ottoman"),
                         model.names = F,
                         #dep.var.caption = "Dependent variable:",
                         dep.var.labels.include = F,
                         omit.stat=c("LL","ser","f", "rsq"), no.space=TRUE, float=FALSE,
                         add.lines=list(c("Model", "OLS", "OLS", "OLS", "OLS", "OLS", "OLS", "OLS", "OLS", "OLS"),
                                        c("Boundary FE", rep("Yes", 9)),
                                        #c("Survey Year FE", rep("Yes", 9)),
                                        c("Demographic Covariates", rep("Yes", 9))))
note_latex <- "\\multicolumn{10}{l} {\\parbox[t]{30cm}{ \\footnotesize \\textit{Notes:}
Demographic controls include: age, gender, urban/rural location.
Geographic controls include: latitude and longitude, distance to the border and border fixed effects.
Coefficients and robust standard errors in parantheses from OLS regression. 
*** = p<.001; ** = p<.01, *=p<.05, +=p<.10.}}"
public_opinion [grepl("Note", public_opinion)] <- note_latex
cat(public_opinion, file="./Paper/tables/tableA6.tex", sep="\n")



################
#Social Capital#
################

#Model1
#/* People you meet first time */
res_q405c<-felm(q405c~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name,
              data=dta)
summary(res_q405c)
res_q405c_se<-res_q405c$cse

#Model2
#/* wallet returned  */
res_q423<-felm(q423~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
               |0|0|PSU_name,
              data=dta)
summary(res_q423)
res_q423_se<-res_q423$cse

#Model3
#/* risk acceptance */
res_q428<-felm(q428~treat + point_x + point_y + 
               Ott_Habs_brd_distance + 
               age_pr + gender_pr + q109_1 + urban + 
               bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
               |0|0|PSU_name,
             data=dta)
summary(res_q428)
res_q428_se<-res_q428$cse

#Model4
#/* what it takes to succeed*/
#  *gen workeffort = .
#*replace workeffort = 1 if q409==1 | q409==2
#*replace workeffort = 0 if q409==3 | q409==4
res_workeffort <- glm(workeffort ~treat + point_x + point_y + 
                 Ott_Habs_brd_distance + 
                 age_pr + gender_pr + q109_1 + urban + 
                 bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7,
               data=dta, family = "binomial")

summary(res_workeffort)

workeffort_coef<-summary(res_workeffort)$coefficients[colnames(summary(res_workeffort)$coefficients)=="Estimate"]
clust.bs.p <- cluster.bs.glm(res_workeffort, dta, ~ PSU_name, report = T, boot.reps = 100, cluster.se = TRUE)
workeffort_lb<-clust.bs.p$ci[,1]
workeffort_ub<-clust.bs.p$ci[,2]

res_workeffort_no<-(workeffort_lb-workeffort_coef)/(-1.96)
res_workeffort_names<-rownames(summary(res_workeffort)$coefficients)
res_workeffort_se<-setNames(res_workeffort_no, res_workeffort_names)
rm(workeffort_coef,
   clust.bs.p,
   workeffort_lb,
   workeffort_ub,
   res_workeffort_no, res_workeffort_names)


#Model5
#/* democracy , more support, significant. */
res_democracy <- glm(democracy ~treat + point_x + point_y + 
                        Ott_Habs_brd_distance + 
                        age_pr + gender_pr + q109_1 + urban + 
                        bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7,
                      data=dta, family = "binomial")
summary(res_democracy)

democracy_coef<-summary(res_democracy)$coefficients[colnames(summary(res_democracy)$coefficients)=="Estimate"]
clust.bs.p <- cluster.bs.glm(res_democracy, dta, ~ PSU_name, report = T, boot.reps = 100, cluster.se = TRUE)
democracy_lb<-clust.bs.p$ci[,1]
democracy_ub<-clust.bs.p$ci[,2]

res_democracy_no<-(democracy_lb-democracy_coef)/(-1.96)
res_democracy_names<-rownames(summary(res_democracy)$coefficients)
res_democracy_se<-setNames(res_democracy_no, res_democracy_names)
rm(democracy_coef,
   clust.bs.p,
   democracy_lb,
   democracy_ub,
   res_democracy_no, res_democracy_names)


#Model6
#/* competition */
#Competition\\is\\Harmful
res_q417c<-felm(q417c~treat + point_x + point_y + 
               Ott_Habs_brd_distance + 
               age_pr + gender_pr + q109_1 + urban + 
               bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
               |0|0|PSU_name,
             data=dta)
summary(res_q417c)
res_q417c_se<-res_q417c$cse

#Model7
#Market economy
#Private\\Ownership is\\Good
dta3$q417b<-ifelse(dta3$q417b<=0 | dta3$q417b>11, NA, dta3$q417b)
res_q417b<-felm(q417b~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name,
              data=dta)
summary(res_q417b)
res_q417b_se<-res_q417b$cse

#Model8
#Market\\Economy is\\Good
res_marketeconomy <- glm(marketeconomy ~treat + point_x + point_y + 
                       Ott_Habs_brd_distance + 
                       age_pr + gender_pr + q109_1 + urban + 
                       bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7,
                     data=dta, family = "binomial")
marketeconomy_coef<-summary(res_marketeconomy)$coefficients[colnames(summary(res_marketeconomy)$coefficients)=="Estimate"]
clust.bs.p <- cluster.bs.glm(res_marketeconomy, dta, ~ PSU_name, report = T, boot.reps = 100, cluster.se = TRUE)
marketeconomy_lb<-clust.bs.p$ci[,1]
marketeconomy_ub<-clust.bs.p$ci[,2]

res_marketeconomy_no<-(marketeconomy_lb-marketeconomy_coef)/(-1.96)
res_marketeconomy_names<-rownames(summary(res_marketeconomy)$coefficients)
res_marketeconomy_se<-setNames(res_marketeconomy_no, res_marketeconomy_names)
rm(marketeconomy_coef,
   clust.bs.p,
   marketeconomy_lb,
   marketeconomy_ub,
  res_marketeconomy_no, res_marketeconomy_names)

#Model 9
#Trust Government
res_q404b<-felm(q404b~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name,
              data=dta)
summary(res_q404b)
res_q404b_se<-res_q404b$cse

#Model 10
#Trust Army
res_q404h<-felm(q404h~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name,
              data=dta)
summary(res_q404h)
res_q404h_se<-res_q404h$cse


#Model 11
#Trust Banks
res_q404j<-felm(q404j~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name,
              data=dta)
summary(res_q404j)
res_q404j_se<-res_q404j$cse

#Model 12
#Trust Foreign Investors
res_q404k<-felm(q404k~treat + point_x + point_y + 
                Ott_Habs_brd_distance + 
                age_pr + gender_pr + q109_1 + urban + 
                bfe1 + bfe2 + bfe3 + bfe4 + bfe5 + bfe6 + bfe7
                |0|0|PSU_name,
              data=dta)
summary(res_q404k)
res_q404k_se<-res_q404k$cse

public_opinion2=stargazer(res_q405c,
                          res_q423,
                          res_q428,
                          res_workeffort,
                          res_democracy,
                          res_q417c,
                          res_q417b,
                          res_marketeconomy,
                          res_q404b,
                          res_q404h,
                          res_q404j,
                          res_q404k,
                         object.names = F,
                         column.labels= c("\\shortstack{Trust Peple\\\\You Meet\\\\First Time}",
                                          "\\shortstack{Likelihood\\\\of Returning\\\\Wallet}",
                                          "\\shortstack{Willingness\\\\to Take\\\\Risks}",
                                          "\\shortstack{Need Effort\\\\and Intelligence\\\\to Succeed}",
                                          "\\shortstack{Democracy\\\\is\\\\Preferable}",
                                          "\\shortstack{Competition\\\\is\\\\Harmful}",
                                          "\\shortstack{Private\\\\Ownership is\\\\Good}",
                                          "\\shortstack{Market\\\\Economy is\\\\Good}",
                                          "\\shortstack{Trust\\\\Government}",
                                          "\\shortstack{Trust\\\\Army}",
                                          "\\shortstack{Trust\\\\Banks}",
                                          "\\shortstack{Trust\\\\Foreign\\\\Investors}"),
                         keep = c("treat"),
                         covariate.labels=c("Ottoman"),
                         model.names = F,
                         #dep.var.caption = "Dependent variable:",
                         dep.var.labels.include = F,
                         se=list(res_q405c_se,
                                 res_q423_se,
                                 res_q428_se,
                                 res_workeffort_se,
                                 res_democracy_se,
                                 res_q417c_se,
                                 res_q417b_se,
                                 res_marketeconomy_se,
                                 res_q404b_se,
                                 res_q404h_se,
                                 res_q404j_se,
                                 res_q404k_se),
                         star.char = c("+", "*", "**", "***"),
                         star.cutoffs = c(0.1, 0.05, 0.01, 0.001),
                         omit.stat=c("LL","ser","f", "rsq"), no.space=TRUE, float=FALSE,
                         add.lines=list(c("Model", "OLS", "OLS", "OLS", "Logit", "Logit", "OLS", "OLS", "Logit", "OLS", "OLS", "OLS", "OLS"),
                                        c("Boundary FE", rep("Yes", 12)),
                                        #c("Survey Year FE", rep("Yes", 12)),
                                        c("Demographic Covariates", rep("Yes", 12))))
note_latex <- "\\multicolumn{13}{l} {\\parbox[t]{33cm}{ \\footnotesize \\textit{Notes:}
Demographic controls include: age, gender, urban/rural location.
Geographic controls include: latitude and longitude, distance to the border and border fixed effects.
Coefficients and robust standard errors in parantheses from OLS regression. 
*** = p<.001; ** = p<.01, *=p<.05, +=p<.10.}}"
public_opinion2 [grepl("Note", public_opinion2)] <- note_latex
cat(public_opinion2, file="./Paper/tables/tableA7.tex", sep="\n")